package org.cos.verifier.ui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;

import org.cos.verifier.automata.Automata;
import org.cos.verifier.automata.AutomataParser;
import org.cos.verifier.utility.IOWrapper;


public class MainDlg extends Frame {
	Button check_btn ;
	Button pref_btn ;
	Button log_btn;
	Button rule_btn;
	
	Panel cmd_panel;
	MainDlg me;
	
	static MainDlg instance = null;
	static MainDlg get_inst (){
		if (instance == null) {
			instance = new MainDlg();
		}
		return instance;
	}
	
	public MainDlg() {
		System.setProperty("file.encoding","UTF-8");
		
		this.setSize(400, 500);
		this.setLayout(new BorderLayout());
		this.me = this;
		
		
		check_btn = new Button("开始检查");
		pref_btn = new Button("首选项");
		log_btn = new Button("查看日志");
		rule_btn = new Button("管理规则");
		
		cmd_panel = new Panel();
		cmd_panel.setLayout(new GridLayout(4,1, 10, 0));
		cmd_panel.setSize(400, 500);
		
		
		// register new Actions Listener
		this.addWindowListener(this.new FrameHandler());
		check_btn.addActionListener(this.new CheckHandler());
		pref_btn.addActionListener(this.new PrefHandler());
		log_btn.addActionListener(this.new ViewLogHandler());
		rule_btn.addActionListener(this.new RuleHandler());
		

		this.add(new Label("Cos-Verifier 模型验证器"), BorderLayout.NORTH);
		cmd_panel.add(check_btn);
		cmd_panel.add(pref_btn);
		cmd_panel.add(log_btn);
		cmd_panel.add(rule_btn);
		me.setTitle("Cos-Verifier 模型验证器");
		this.add(cmd_panel, BorderLayout.CENTER);
		this.add(new Label(), BorderLayout.SOUTH);
	}

	public MainDlg(GraphicsConfiguration arg0) {
		super(arg0);
	}

	public MainDlg(String arg0) {
		super(arg0);
	}

	public MainDlg(String arg0, GraphicsConfiguration arg1) {
		super(arg0, arg1);
	}
	

	
	class CheckHandler implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			LogDlg.get_inst().log("[i]Click check button!");
			FileDialog fd = new FileDialog(me, "选择文件");
			fd.setVisible(true);
			String path = fd.getDirectory();
			String name = fd.getFile();
			if (path == null || name == null) {
				return;
			}
			fd.dispose();
			LogDlg.get_inst().log("[!]get file:" + path + name);

			CheckProgressDlg check_progress= new CheckProgressDlg();
			check_progress.display(path + name);
		}
	}
	
	class PrefHandler implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			LogDlg.get_inst().log("[i]Click Options button!");
			PreferenceDlg pref_dlg = new PreferenceDlg();
			pref_dlg.display();
		}
	}
	
	class RuleHandler implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			LogDlg.get_inst().log("[i]Click Rule button!");
			RuleMgrDlg rule_mgr = new RuleMgrDlg();
			rule_mgr.display();
		}
	}
	
	class ViewLogHandler implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			LogDlg.get_inst().log("[i]Open Log Dialog");
			LogDlg.get_inst().display();
		}
	}
	
	class FrameHandler extends WindowAdapter {
		public void windowClosing(WindowEvent e) {
			System.exit(0);
		}
	}
}
